package com.dr.dac.knowledgebase.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.dr.dac.knowledgebase.R;
import com.dr.dac.knowledgebase.ui.bean.KFunction;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weifeiyang on 2016/12/26.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    private Context mContext;
    private List<KFunction> list ;
    private MyItemClickListener myItemClickListener;
    public MyAdapter(Context mContext){
      list = new ArrayList<>();
      this.mContext = mContext;
    }

    public void setDatas(List<KFunction> list ){
        this.list = list;
    }

    public void setOnItemClickListener(MyItemClickListener listener){
        this.myItemClickListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = null;
        holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_main, parent,
                false),myItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position).getFPointIDName());
        holder.keywordTv.setText(list.get(position).getKeyWord());
        holder.id.setText(list.get(position).getClassSID());
        if(position==0){
            holder.topLayout.setVisibility(View.VISIBLE);
        }else{
            holder.topLayout.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {

        TextView tv;
        TextView id;
        TextView keywordTv;
        View topLayout;
        View clickLayout;
        private MyItemClickListener listener;

        public MyViewHolder(View view,MyItemClickListener clickListener)
        {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv);
            id = (TextView) view.findViewById(R.id.id);
            clickLayout = view.findViewById(R.id.click_layout);
            keywordTv = (TextView) view.findViewById(R.id.keyword_tv);
            topLayout = view.findViewById(R.id.top_layout);
            this.listener = clickListener;
            clickLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(listener!=null){
                listener.onItemClick(v,getPosition());
            }
        }
    }

    public interface  MyItemClickListener{
        public void onItemClick(View view, int position);
    }
}
